global root "C:\Users\hanwa\Desktop\replication"

use "$root\data\IMC_HA_Monthly_Panel_clean.dta", replace

***Figure 4A-Number of days per case, new claims
preserve
g month=yearmonth-(year*100)
g year_month=ym(year,month)
g bens_year=int(startdate/100)
g bens_month=startdate-(bens_year*100)
g time_bens_intro=ym(bens_year,bens_month)

drop if startdate==-1
keep if cem_matched==1
gen bench_time_mon = year_month-time_bens_intro
gen bench_time_mon_0=bench_time_mon==-3
foreach num of numlist 1(1)22{
gen bench_time_mon_p`num'= bench_time_mon == `num'-3
}

foreach num of numlist 1(1)11{
gen bench_time_mon_m`num'= bench_time_mon == -`num'-3
}

gen bench_time_mon_m12= bench_time_mon <= -12-3 & bench_time_mon!=.
gen bench_time_mon_p23= bench_time_mon >= 23-3 & bench_time_mon!=.

drop bench_time_mon

global prepost bench_time_mon_m12 bench_time_mon_m11 bench_time_mon_m10 bench_time_mon_m9 bench_time_mon_m8 bench_time_mon_m7 bench_time_mon_m6 bench_time_mon_m5 bench_time_mon_m4 bench_time_mon_m3 bench_time_mon_m2  zero bench_time_mon_0 bench_time_mon_p* 
gen zero = 0 


est clear
stackedev hb_newclaim_speed $prepost, cohort(startdate) time(region_ym) never_treat(no_treat) unit_fe(code) clust_unit(code)  covariates (deficit_rate grant_dependence lnpop lnclaim lnwage  kids private umemploy pension vulnerable )
eststo f1

graph drop _all
coefplot f1 ///
,omitted keep(bench_time_mon_m12 bench_time_mon_m11 bench_time_mon_m10 bench_time_mon_m9 bench_time_mon_m8 bench_time_mon_m7 bench_time_mon_m6 bench_time_mon_m5 bench_time_mon_m4 bench_time_mon_m3 bench_time_mon_m2 zero bench_time_mon_0 bench_time_mon_p*)  ///    
vertical lcolor(gs6) mlcolor(gs6) mfcolor(gs6)  msize (*1.6) msymbol(Oh)  ///
legend(off) ///
scheme(s1mono) ///
graphregion(margin(5 9 5 5)) ///
xlabel( 1 "-12" 3 "-10" 5 "-8" 7 "-6" 9 "-4" 11 "-2" 13 "0" 15 "2" 17 "4" 19 "6" 21 "8" 23 "10" 25 "12" 27 "14" 29 "16" 31 "18" 33 "20" 35 "22"  ) ///
ytitle("Number of days per case, new claims", margin(0 1 4 0)) ///
xtitle("Month") ///
yline(0, lcolor(red) lwidth(*1.1)) ///
xline(13, lwidth(medium) lpattern(dash) lcolor(cranberry)) ///
xline(25, lwidth(medium) lpattern(dash) lcolor(cranberry)) ///
text( 6 7  ///
"IMC" ///
, place(se) box margin(l+4 t+1 b+1) width(34))
graph export "$root\figures\figure4A.png", replace
restore

***Figure 4B-Number of days per case, change of circumstances
preserve
g month=yearmonth-(year*100)
g year_month=ym(year,month)
g bens_year=int(startdate/100)
g bens_month=startdate-(bens_year*100)
g time_bens_intro=ym(bens_year,bens_month)

drop if startdate==-1
keep if cem_matched==1
gen bench_time_mon = year_month-time_bens_intro
gen bench_time_mon_0=bench_time_mon==-3
foreach num of numlist 1(1)22{
gen bench_time_mon_p`num'= bench_time_mon == `num'-3
}

foreach num of numlist 1(1)11{
gen bench_time_mon_m`num'= bench_time_mon == -`num'-3
}

gen bench_time_mon_m12= bench_time_mon <= -12-3 & bench_time_mon!=.
gen bench_time_mon_p23= bench_time_mon >= 23-3 & bench_time_mon!=.

drop bench_time_mon

global prepost bench_time_mon_m12 bench_time_mon_m11 bench_time_mon_m10 bench_time_mon_m9 bench_time_mon_m8 bench_time_mon_m7 bench_time_mon_m6 bench_time_mon_m5 bench_time_mon_m4 bench_time_mon_m3 bench_time_mon_m2  zero bench_time_mon_0 bench_time_mon_p* 
gen zero = 0 

est clear

stackedev hb_coc_speed $prepost, cohort(startdate) time(region_ym) never_treat(no_treat) unit_fe(code) clust_unit(code)  covariates (deficit_rate grant_dependence lnpop lnclaim lnwage  kids private umemploy pension vulnerable )
eststo f2

graph drop _all
coefplot f2 ///
,omitted keep(bench_time_mon_m12 bench_time_mon_m11 bench_time_mon_m10 bench_time_mon_m9 bench_time_mon_m8 bench_time_mon_m7 bench_time_mon_m6 bench_time_mon_m5 bench_time_mon_m4 bench_time_mon_m3 bench_time_mon_m2 zero bench_time_mon_0 bench_time_mon_p*)  ///    
vertical lcolor(gs6) mlcolor(gs6) mfcolor(gs6)  msize (*1.6) msymbol(Oh)  ///
legend(off) ///
scheme(s1mono) ///
graphregion(margin(5 9 5 5)) ///
xlabel( 1 "-12" 3 "-10" 5 "-8" 7 "-6" 9 "-4" 11 "-2" 13 "0" 15 "2" 17 "4" 19 "6" 21 "8" 23 "10" 25 "12" 27 "14" 29 "16" 31 "18" 33 "20" 35 "22"  ) ///
ytitle("Number of days per case, change of circumstances", margin(0 1 4 0)) ///
xtitle("Month") ///
yline(0, lcolor(red) lwidth(*1.1)) ///
xline(13, lwidth(medium) lpattern(dash) lcolor(cranberry)) ///
xline(25, lwidth(medium) lpattern(dash) lcolor(cranberry)) ///
text( 6 7  ///
"IMC" ///
, place(se) box margin(l+4 t+1 b+1) width(34))
graph export "$root\figures\figure4B.png", replace
restore
